//
//  NBADBManager+Worker.swift
//  NBA
//
//  Created by tiger on 2018/2/3.
//  Copyright © 2018年 Meigabay. All rights reserved.
//

import UIKit
import SQLite

internal class NBAWorkerTableColumn: NSObject {
    let workerId = Expression<String>("workerId")
    let avatar = Expression<String>("avatar")
    let name = Expression<String>("name")
    let nickname = Expression<String>("nickname")
    let gender = Expression<Int>("gender")
    let height = Expression<Double>("height")
    let weight = Expression<Double>("weight")
    let birthDay = Expression<Double>("birthDay")
    let drafted = Expression<Double>("drafted")
    let introduction = Expression<String>("introduction")
    let createTime = Expression<Double>("createTime")
    let type = Expression<Int>("type")
}

extension NBADBManager {
    internal func createWorkerTable() throws {
        let playerColumns = NBAPlayerTableColumn()
        let createWorkerTable = self.nbaWorkerTable.create(ifNotExists: true) { (table) in
            table.column(playerColumns.workerId,primaryKey: true)
            table.column(playerColumns.avatar)
            table.column(playerColumns.name)
            table.column(playerColumns.nickname)
            table.column(playerColumns.gender)
            table.column(playerColumns.height)
            table.column(playerColumns.weight)
            table.column(playerColumns.birthDay)
            table.column(playerColumns.drafted)
            table.column(playerColumns.introduction)
            table.column(playerColumns.createTime)
            table.column(playerColumns.type)
        }
        do {
            try self.db.run(createWorkerTable)
            LogUtil.i("created worker table")
        } catch {
            LogUtil.e(error)
            throw(error)
        }
    }
    
    internal func insertWorker(worker: NBAWorker) throws {
        let columns = NBAWorkerTableColumn()
        let insertWorker = self.nbaWorkerTable.insert(columns.workerId <- worker.workerId,
                                                  columns.avatar <- worker.avatar,
                                                  columns.name <- worker.name,
                                                  columns.nickname <- worker.nickname,
                                                  columns.gender <- worker.gender.rawValue,
                                                  columns.height <- worker.height,
                                                  columns.weight <- worker.weight,
                                                  columns.birthDay <- worker.birthDay,
                                                  columns.drafted <- worker.drafted,
                                                  columns.introduction <- worker.introduction,
                                                  columns.createTime <- Date().timeIntervalSince1970,
                                                  columns.type <- worker.type.rawValue)
        do {
            try self.db.run(insertWorker)
            LogUtil.i("Insert Boss Success")
        } catch {
            LogUtil.e(error)
            throw(error)
        }
    }
    
    
    /// 裁判组
    ///
    /// - Returns: 裁判
    func referees() -> [NBAReferee] {
            let workerColums = NBAWorkerTableColumn()
            let filterWorker = self.nbaWorkerTable.filter(workerColums.type == NBAWorkerType.referee.rawValue)
            do {
                let results = try self.db.prepare(filterWorker)
                var workers = [NBAReferee]()
                for result in results {
                    let worker = NBAReferee(result: result)
                    workers.append(worker)
                }
                return workers
            } catch {
                LogUtil.i(error)
                return []
            }
    }
}
